//
//  ViewController.m
//  Pong
//
//  Created by iD Student on 7/11/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    ball=[[Ball alloc] init];
    [self.view addSubview:ball.image];
    lastScorer=2;
    [self.view setBackgroundColor:[UIColor blackColor]];
    [self.view setMultipleTouchEnabled:true];
    paddle1.image=[UIImage imageNamed:@"Pong Paddle.png"];
    paddle2.image=[UIImage imageNamed:@"Pong Paddle.png"];
}
-(IBAction)playTwoPlayerPressed:(id)sender
{
    play2PlayerButton.hidden = YES;
    [self resetBall];
    isTwoPlayer=true;
    gameTimer = [NSTimer scheduledTimerWithTimeInterval:.033 target:self selector:@selector(updateGame) userInfo:nil repeats:true];
}
-(void) resetBall
{
    ball.image.center = CGPointMake(160, 240);
    
    ball.velocityX = 0;
    if(lastScorer == 1) //Bottom Player scores
    {
        ball.velocityY = -5;
    }
    else if (lastScorer == 2) //Top Player scores
    {
        ball.velocityY = 5;
    }
}
-(void) updateGame
{
    [self moveBall];
    [self checkForGoal];
}
-(void) moveBall
{
    CGPoint oldCenter = ball.image.center;
    ball.image.center = CGPointMake(oldCenter.x + ball.velocityX, oldCenter.y +ball.velocityY);
    
    bool didBallHitPaddle = false;
    if(CGRectIntersectsRect(paddle1.frame, ball.image.frame))
    {
        CGRect ballFrame = ball.image.frame;
        ball.image.center = CGPointMake(ball.image.center.x, paddle1.frame.origin.y - ballFrame.size.height/2);
        didBallHitPaddle = true;
    }
    else if (CGRectIntersectsRect(paddle2.frame, ball.image.frame))
             {
                 CGRect ballFrame = ball.image.frame;
                 ball.image.center = CGPointMake(ball.image.center.x, paddle1.frame.origin.y - ballFrame.size.height/2);
                 didBallHitPaddle = 2;
             }
}
-(void) checkForGoal
{
    if (CGRectIntersectsRect(ball.image.frame, topGoal.frame))
    {
        player1Score++;
        lastScorer = 2;
        [self resetBall];
        NSLog(@"player 1 scored");

    }
    else if (CGRectIntersectsRect(ball.image.frame, bottomGoal.frame))
    {
        player2Score++;
        lastScorer = 1;
        [self resetBall];
         NSLog(@"player 2 scored");

    }
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for(UITouch *touch in touches)
    {
        CGPoint point = [touch locationInView:self.view];
        if (isTwoPlayer) {
            if(point.y > 240)
            {
                //The bottom of the screen is for player 1
                paddle1.center = CGPointMake(point.x, paddle1.center.y);
            }
            else if ( point.y <240)
            {
                //The top of the screen is for player 2
                paddle2.center = CGPointMake(point.x, paddle2.center.y);
            }
            
        }
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
